Page({
  data: {
    marketList: [],
    addressList: [],
    addressId:'',
    isShow : false 
  },
  onLoad:function(){

  },
  onShow: function () {
    this.setData({
      isShow: false
    })
    this.getWxInfo();
  },
  getWxInfo() {
    wx.showLoading({
      title: '加载中..',
    })
    var me = this;
    me.setData({
      layerflage:1
    })
    wx.login({
      success(res) {
        var wxcode = res.code;
        wx.getUserInfo({
          success(res) {
            me.getReUserInfo(wxcode, res.encryptedData, res.iv)
          },
          fail(res) {
            console.log(res)
            wx.redirectTo({
              url: '../wxauthorized/wxauthorized',
            })
          }
        })
      }
    })
  },
  getReUserInfo: function (wxsmallcode, encrypted, iv) {
    var me =this;
    var url = getApp().data.reUrl + 'wxopenidService/wxxcxautoregister';
    var dataObj = {
      WXSmallCode: wxsmallcode,
      encryptedData: encrypted,
      iv: iv,
      systemFlag: 'rueMall'
    }
    wx.request({
      url: url,
      data: dataObj,
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        wx.setStorageSync('userid', res.data.response.userId);
        wx.setStorageSync('token', res.data.response.token);
        wx.setStorageSync('loginTime', res.data.response.loginTime);
        me.getAddressList();
      },
      fail: function (res) {
        console.log(res);
        this.setData({
          layerflage:0
        })
      }
    })
  },
  //收货地址相关
  getAddressList() {
    var url = getApp().data.url + 'userexpand/getAddress';
    var body = {};
    getApp().wxAjax(this.spliceAddress, url, body);
  },
  spliceAddress(res) {
    var resObj = res.data.body;
    console.log(resObj)
    var addressList;
    if (typeof resObj.address_list == "undefined"){
      addressList = 0;
    }else{
      var sLocation = wx.getStorageSync('selectLoction')
      if (sLocation == ""){
        addressList = resObj.address_list[0];
      }else{
        addressList = sLocation;
      }
      
    }
    console.log(addressList)
    var addressId = addressList.address_id;
    if (typeof addressId == 'undefined'){
      addressId:''
    }
    this.setData({
      addressList: addressList,
      addressId: addressList.address_id
    })
    this.getAddressTaW(addressList);
  },
  //超市列表相关
  getAddressTaW(addressObj) {
    var me = this;
    var tawData = {};
    if (addressObj == 0) {
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          tawData.through = res.longitude;
          tawData.weft = res.latitude;
          me.getStoreListinfo(tawData);
        }
      })
    }else{
      tawData.through = addressObj.through;
      tawData.weft = addressObj.weft;
      me.getStoreListinfo(tawData);
    }

  },
  getStoreListinfo(tawData) {
    var url = getApp().data.url + 'sysinfo/getSupermarketInfoByLAL';
    var body = tawData;
    getApp().wxAjax(this.spliceSupermareke, url, body);

  },
  spliceSupermareke: function (res) {
    var resObj = res.data;
    console.log(resObj.supermarkets)
    if (resObj.header.res_code == 0) {
      this.setData({
        marketList: resObj.body.supermarkets
      })
    } else {
      wx.showToast({
        title: resObj.header.message,
        icon: 'none'
      })
    }
    this.setData({
      isShow: true
    })
  },
  toaddressList(){
    wx.navigateTo({
      url: '../addressList/addressList?form=index',
    })
  },
  toeindex(e){
    var marketList = this.data.marketList;
    var index = e.currentTarget.dataset.index;
    var supermarket_id;
    var seller_id;
    var supername;
    var superaddress;
    for (let i = 0; i < marketList.length; i++) {
      if (index == i) {
        supermarket_id = marketList[i].supermarket_id;
        seller_id = marketList[i].seller_id;
        supername = marketList[i].supermarket_name;
        superaddress = marketList[i].supermarket_address;
        break;
      }
    }
    wx.setStorageSync('supermarket_id', supermarket_id);
    wx.setStorageSync('seller_id', seller_id);
    wx.setStorageSync('supername', supername);
    wx.setStorageSync('superaddress', superaddress);
    wx.setStorageSync('addressId', this.data.addressId)
    wx.removeStorageSync('selectLoction');
    wx.removeStorageSync('indexObj');
    wx.removeStorageSync('fromFlage');
    wx.reLaunch({
      url: '../eIndex/eIndex'
    })
  },
  toAddAddress(){
    wx.setStorageSync('fromFlage', 'index')
    wx.navigateTo({
      url: '../addAddress/addAddress?pageFrom=index'
    })
  }
})